package com.gomcarter.frameworks.base.config;

import com.gomcarter.frameworks.base.converter.CustomMappingJacksonConverter;
import com.google.common.base.Strings;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;

public class BaseMvcConfigurer implements WebMvcConfigurer {
    private final String indexLocation;
    private final CustomMappingJacksonConverter customMappingJacksonConverter;

    public BaseMvcConfigurer(String indexLocation, CustomMappingJacksonConverter customMappingJacksonConverter) {
        if(Strings.isNullOrEmpty(indexLocation)) {
            this.indexLocation = BaseConfConst.ROOT_PATH + BaseConfConst.BASE_INDEX_HTML;
        } else {
            if(!indexLocation.startsWith(BaseConfConst.ROOT_PATH)) {
                indexLocation = BaseConfConst.ROOT_PATH + indexLocation;
            }
            this.indexLocation = indexLocation;
        }
        this.customMappingJacksonConverter = customMappingJacksonConverter;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(BaseConfConst.ROOT_PATH).setViewName(indexLocation);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
        while (iterator.hasNext()) {
            HttpMessageConverter<?> messageConverter = iterator.next();
            if (messageConverter instanceof StringHttpMessageConverter) {
                Charset charset = ((StringHttpMessageConverter) messageConverter).getDefaultCharset();
                if (!Charset.defaultCharset().equals(charset)) {
                    iterator.remove();
                }
            }
            if (messageConverter instanceof MappingJackson2HttpMessageConverter) {
                iterator.remove();
            }
        }
        converters.add(0, customMappingJacksonConverter);
    }
}
